# This file is part of the Net::Tube module.
#
# Net::Tube is Free Software; you can use, redistribute it and/or
# modify it under the terms of either:
#
# a) the GNU General Public License as published by the Free Software
#    Foundation, either version 2, or (at your opinion) any later
#    version, or
#
# b) the "Artistic License" which comes with Perl.
#
use strict;
use warnings;
use Test::Simple tests => 4;

use Net::Tube::ConfigLoader;
use XML::LibXML;

use Data::Dumper;

our $tconfig = 'tdata/machine_config1.xml';

sub test_new {
    my $t;
    eval { $t = Net::Tube::ConfigLoader->new(); };
    if ($@) {
        print "# cannot initiate a new instance $@\n";
        return 0;
    }

    return 1;
}

ok( test_new(), "test new" );

sub test_process_config {
    my $cl  = Net::Tube::ConfigLoader->new();
    my $p   = XML::LibXML->new();
    my $doc = $p->parse_file($tconfig);

    unless ($doc) {
        print "# cannot load the test file!?\n";
        return 0;
    }

    my $t = $cl->process_config( $doc->documentElement );
    unless ( defined $t ) {
        print "# nothing has been processed \n";
        if ( $cl->{tube_error} ) {
            print "# - detected tube error: " . $cl->{tube_error} . "\n";
        }
        return 0;
    }

    unless ( scalar(@$t) == 3 ) {
        print "# wrong number of filters has been found! got "
          . scalar(@$t)
          . " expected 3\n";
        return 0;
    }

    unless ( $t->[0]->{name} && $t->[0]->{name} eq "Net::Tube::Loader::File" ) {
        print "# first filter has a bad name\n";
        return 0;
    }
    unless ( defined $t->[0]->{parameters}->{loadSource}
        && scalar @{ $t->[0]->{parameters}->{loadSource} } == 1
        && $t->[0]->{parameters}->{loadSource}->[0] eq
        'tdata/local_config.xml' )
    {
        print "# first filter has an unexpected parameter\n";
        print "# " . $t->[0]->{parameters}->{loadSource}->[0] . "\n";
        return 0;
    }
    unless ( $t->[1]->{name} && $t->[1]->{name} eq "Net::Tube::Loader::File" ) {
        print "# second filter has a bad name\n";
        return 0;
    }
    unless ( defined $t->[1]->{parameters}->{loadXPath}
        && scalar @{ $t->[1]->{parameters}->{loadXPath} } == 1
        && $t->[1]->{parameters}->{loadXPath}->[0] eq '/config/file' )
    {
        print "# second filter has an unexpected parameter\n";
        return 0;
    }
    unless ( $t->[2]->{name}
        && $t->[2]->{name} eq "Net::Tube::Filter::SimpleMerge" )
    {
        print "# third filter has a bad name\n";
        return 0;
    }
    unless (
           defined $t->[2]->{parameters}->{mergerElement}
        && scalar @{ $t->[2]->{parameters}->{mergerElement} } == 1
        && ref( $t->[2]->{parameters}->{mergerElement}->[0] ) eq    
        "XML::LibXML::Element"
      )
    {
        print "# thrid filter has an unexpected parameter\n";
        return 0;
    }

    return 1;
}

ok( test_process_config(), "process config from DOM node" );

sub test_init_tube {
     my $cl  = Net::Tube::ConfigLoader->new();
    my $p   = XML::LibXML->new();
    my $doc = $p->parse_file($tconfig);

    unless ($doc) {
        print "# cannot load the test file!?\n";
        return 0;
    }

    my $cfg = $cl->process_config( $doc->documentElement );
    unless ( defined $cfg ) {
        print "# nothing has been processed \n";
        if ( $cl->{tube_error} ) {
            print "# - detected tube error: " . $cl->{tube_error} . "\n";
        }
        return 0;
    }
    
    my $tube = $cl->init_tube($cfg);
    
    unless ( defined $tube ) {
        print "# no tube has been created \n";
        if ( $cl->{tube_error}) {
            print "# detected tube error ". $cl->{tube_error} . "\n";
        }
        return 0;
    }
    
    unless ( scalar @$tube == 3 ) {
        print "# wrong number of filters has been found! got "
          . scalar(@$tube)
          . " expected 3\n";
        return 0;
    }
    
    unless ( ref($tube->[0]) eq "Net::Tube::Loader::File" ) {
        print "# detected a wrong filter at the first position\n";
        return 0;
    }
    unless ( ref($tube->[1]) eq "Net::Tube::Loader::File" ) {
        print "# detected a wrong filter at the second position\n";
        return 0;
    }
    unless ( ref($tube->[2]) eq "Net::Tube::Filter::SimpleMerge" ) {
        print "# detected a wrong filter at the third position\n";
        return 0;
    }
    return 1;
}

ok( test_init_tube(), "init the tube" );

sub test_load_file {
    my $cl  = Net::Tube::ConfigLoader->new();
    my $tube = $cl->load_config_file($tconfig);
    unless ( defined $tube ) {
        print "# no tube has been created \n";
        if ( $cl->{tube_error}) {
            print "# detected tube error ". $cl->{tube_error} . "\n";
        }
        return 0;
    }
    
    unless ( scalar @$tube == 3 ) {
        print "# wrong number of filters has been found! got "
          . scalar(@$tube)
          . " expected 3\n";
        return 0;
    }
    
    unless ( ref($tube->[0]) eq "Net::Tube::Loader::File" ) {
        print "# detected a wrong filter at the first position\n";
        return 0;
    }
    unless ( ref($tube->[1]) eq "Net::Tube::Loader::File" ) {
        print "# detected a wrong filter at the second position\n";
        return 0;
    }
    unless ( ref($tube->[2]) eq "Net::Tube::Filter::SimpleMerge" ) {
        print "# detected a wrong filter at the third position\n";
        return 0;
    }
    
    return 1;
}
ok( test_load_file(), "load file (combine it all)" );

